package com.ruoyi.patrol.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.patrol.domain.PatrolTask;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.service.IPatrolTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 巡查任务Controller
 * 
 * @author yepanpan
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/patrol/task")
public class PatrolTaskController extends BaseController
{
    @Autowired
    private IPatrolTaskService patrolTaskService;

    /**
     * 查询巡查任务列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(PatrolTask patrolTask)
    {
        startPage();
        List<PatrolTask> list = patrolTaskService.selectPatrolTaskList(patrolTask);
        return getDataTable(list);
    }

    /**
     * 导出巡查任务列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:task:export')")
    @Log(title = "巡查任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatrolTask patrolTask)
    {
        List<PatrolTask> list = patrolTaskService.selectPatrolTaskList(patrolTask);
        ExcelUtil<PatrolTask> util = new ExcelUtil<PatrolTask>(PatrolTask.class);
        util.exportExcel(response, list, "巡查任务数据");
    }


    /**
     * 获取巡查任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(patrolTaskService.selectPatrolTaskById(id));
    }

    /**
     * 新增巡查任务
     */
    @PreAuthorize("@ss.hasPermi('patrol:task:add')")
    @Log(title = "巡查任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PatrolTask patrolTask)
    {
                patrolTask.setCreateBy(getUsername());
        return toAjax(patrolTaskService.insertPatrolTask(patrolTask));
    }

    /**
     * 修改巡查任务
     */
    @PreAuthorize("@ss.hasPermi('patrol:task:edit')")
    @Log(title = "巡查任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PatrolTask patrolTask)
    {
                patrolTask.setUpdateBy(getUsername());
        return toAjax(patrolTaskService.updatePatrolTask(patrolTask));
    }

    /**
     * 删除巡查任务
     */
    @PreAuthorize("@ss.hasPermi('patrol:task:remove')")
    @Log(title = "巡查任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(patrolTaskService.deletePatrolTaskByIds(ids));
    }


    /**
     * 开始巡查任务
     */
    @GetMapping(value = "/start/{id}")
    public AjaxResult startTask(@PathVariable("id") Long id)
    {
        return success(patrolTaskService.startPatrolTask(id));
    }

    /**
     * 结束巡查任务
     */
    @GetMapping(value = "/stop/{id}")
    public AjaxResult stopTask(@PathVariable("id") Long id)
    {
        return success(patrolTaskService.stopPatrolTask(id));
    }
}
